﻿/**********************************************************************
/// <summary>
// Name of File     : AddUpdateUserPresenter.cs
// Author           : Parminder Singh
// Create date      : 17th Sep 2011
/// </summary>
************************************************************************/

using System.Collections.Generic;
using Crystal.Common.Model;
using CrystalAdmin.BLL;
using System;
using Crystal.CLL;

namespace CrystalAdmin.Presenter
{
    public class AddUpdateUserPresenter : IAddUpdateUserPresenter
    {
        #region Fields

        private IAddUpdateUserView viewAddUpdate;
        private CrystalEntities context;
        #endregion

        #region Constructors

        public AddUpdateUserPresenter(IAddUpdateUserView view)
        {
            this.viewAddUpdate = view;
            this.viewAddUpdate.EveSaveUser += view_AddEditUser;
            this.viewAddUpdate.CheckUserName += view_CheckUserName;
            this.viewAddUpdate.CheckEmailAddress += view_CheckUserEmail;
            context = new CrystalEntities(CommonCLL.mstrContextConnectionstring );
        }
        #endregion

        #region Event Handlers
        
        public void view_AddEditUser()
        {
            using (UserManager clsUserManager = new UserManager())
            {
                clsUserManager.AddEditUser(context, this.viewAddUpdate.CurrentUser);
            }
        }

        public bool view_CheckUserName(string userName)
        {
            using (UserManager clsUserManager = new UserManager())
            {
                string user = string.Empty;
                List<USR_Users> users = new List<USR_Users>();
                users = clsUserManager.CheckUserName(context, userName);
                if (users.Count > 0)
                {
                    user = users[users.Count - 1].UserName;
                    if (!string.IsNullOrEmpty(user))
                        return true;
                }
                return false;
            }
        }

        public int view_CheckUserEmail(string emailId)
        {
            using (UserManager clsUserManager = new UserManager())
            {
                int count = clsUserManager.CheckUserEmail(context, emailId);
                return count;
            }
        }

      
        #endregion
    }
}
